<template>
  <div>
    <el-button type="warning"  size="small" @click="doPrint" >打印</el-button>
    <el-card class="print">
    <div  ref="printId">
      <p id="title"> </p>
        <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="date"
            label="日期"
            width="180"
            align='center'>
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            width="180"
            align='center'>
          </el-table-column>
          <el-table-column
            prop="address"
            label="地址"
            align='center'>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
</div>
</template>

<script>
import printJS from 'print-js';
import html2canvas from 'html2canvas';
export default {
  components:{
  },
  data(){
    return {
      value: new Date(),
      tableData: [{
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }]
    }
  },
  methods:{
    doPrint() {
			const printContent = this.$refs.printId;
			// 获取dom 宽度 高度
			const width = printContent.clientWidth;
			const height = printContent.clientHeight;
			// 创建一个canvas节点
			const canvas = document.createElement('canvas');
		
			const scale = 4; // 定义任意放大倍数，支持小数；越大，图片清晰度越高，生成图片越慢。
			canvas.width = width * scale; // 定义canvas 宽度 * 缩放
			canvas.height = height * scale; // 定义canvas高度 *缩放
			canvas.style.width = width * scale + 'px';
			canvas.style.height = height * scale + 'px';
			canvas.getContext('2d').scale(scale, scale); // 获取context,设置scale
		
			const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 获取滚动轴滚动的长度
			const scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft; // 获取水平滚动轴的长度
			html2canvas(printContent, {
				canvas,
				backgroundColor: null,
				useCORS: true,
				windowHeight: document.body.scrollHeight,
				scrollX: -scrollLeft, // 解决水平偏移问题，防止打印的内容不全
				scrollY: -scrollTop
			}).then((canvas) => {
				const url = canvas.toDataURL('image/png')
				printJS({
				printable: url,
				type: 'image',
				documentTitle: '', // 标题
				style: '@page{size:auto;margin: 0cm 1cm 0cm 1cm;} *{color:#000!important;border-color: #000!important;}' // 去除页眉页脚
				})
			}).catch(err=>{
				console.error(err);
			})
		}

  },
//   watch:{
//     selectedArr:{
//       handler(newval,oldval){
//         $("br").remove();
//         if(newval && newval.length !== 0 ){
//           this.handleShowData(newval)
//         }
//       },
//       deep: true,
//     },
    // checkData:{
    //   handler(newVal){
    //     if(newVal.length !== 0){
    //       this.checkShowData(newVal)
    //     }
    //     // console.log("newVal",newVal)
    //     // console.log(this.date)
    //   },
    //   deep:true
    // },
    // date:{
    //   handler(newVal){
    //     console.log(newVal)
        
    //   },
    //   deep:true
    // },
    
//   }
}
</script>

<style scoped>

*{
  font-size: 16px;
}
b{
  font-weight: bold;
}
thead{
  border-top: 1px solid #000;
  /* border-bottom: 1px solid #000; */
}
.center{
  text-align: center;
  margin: auto;
}
.right{
  text-align: right;
}
.text{
  border-bottom: 3px double black;
  font-size: 18px;
  font-weight: bold;
}
.between{
  display: flex;
  justify-content: space-between;
  margin: 10px auto;
}
.between p{
  flex: 1;
}
table{
  table-layout:fixed;
}
.info{
  width: 80%;
}
.table{
  margin-bottom: 20px;
}
tr{
  height: 30px;
  line-height: 30px;
  border-top: 1px solid black;
}
.top{margin-top: 10px}
@page{
  size: auto A4 landscape;
  margin: 15mm 13mm;
}
@media print{

}
</style>
